/**
 * 
 */
package pyrachnid.view.component;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import pyrachnid.ctrl.Simulator;
import pyrachnid.model.ground_model.Map;

/**
 * @author zygou
 *
 */
public class MapDimDial extends JDialog implements ActionListener, ChangeListener {
	private static final long serialVersionUID = 1L;
	public static final boolean VALIDATED = true;

	private JButton btnVal;
	private JButton btnCan;
	
	private JSpinner dimX;
	private JSpinner dimY;
	private JSpinner nbX;
	private JSpinner nbY;
	
	private boolean isValidated;
	private Simulator _simulator;
	
	private BufferedImage img;
	private double imgRatio;
	private String imgName;
	
	/**
	 * @param simu
	 * @throws IOException 
	 */
	public MapDimDial(Simulator simu, File file) throws IOException {
		super(simu.getMainWindow());
		
		isValidated=false;
		_simulator=simu;
		img = ImageIO.read(file);
		imgRatio = (double) img.getWidth()/img.getHeight();
		imgName = file.getName().split("(?i)\\.png")[0];
		
		setTitle("Information concernant la map");
		setModal(true);
		setPreferredSize(new Dimension(280,230));
		setResizable(false);
		
		Point locMain = simu.getMainWindow().getLocationOnScreen();
		
		setLocation(Math.round((float)(locMain.getX()+simu.getMainWindow().getWidth()/2-getPreferredSize().getWidth()/2)),
				Math.round((float)(locMain.getY()+simu.getMainWindow().getHeight()/2-getPreferredSize().getHeight()/2)));
		
		JPanel panel = new JPanel(new GridBagLayout());
		panel.setPreferredSize(getPreferredSize());
		
		JPanel dimField = new JPanel(new GridBagLayout());
		TitledBorder titleDimPane = BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), "Dimensions (en mètres)");
		titleDimPane.setTitlePosition(TitledBorder.ABOVE_TOP);
		dimField.setBorder(titleDimPane);
		dimField.setPreferredSize(new Dimension(250,90));
		
		JPanel nbField = new JPanel(new GridBagLayout());
		TitledBorder titleNbPane = BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), "Découpage");
		titleNbPane.setTitlePosition(TitledBorder.ABOVE_TOP);
		nbField.setBorder(titleNbPane);
		nbField.setPreferredSize(dimField.getPreferredSize());
		
		GridBagConstraints c = new GridBagConstraints();
		
		//Panel Dimension
		JLabel labDimX = new JLabel("largeur :");
		c.gridx=0; c.gridy=0;
		c.anchor=GridBagConstraints.EAST;
		c.weighty=1;
		dimField.add(labDimX,c);
		
		dimX = new JSpinner();
		dimX.setPreferredSize(new Dimension(70, 25));
		if(imgRatio > 1)
			dimX.setModel(new SpinnerNumberModel((int)Math.round(10*imgRatio), (int)Math.round(10*imgRatio), 10000, 1));
		else
			dimX.setModel(new SpinnerNumberModel(10, 10, (int)Math.round(10000*imgRatio), 1));
		
		c.gridx=1; c.gridy=0;
		c.anchor=GridBagConstraints.CENTER;
		dimField.add(dimX,c);
		
		JLabel labDimY = new JLabel("hauteur :");
		c.gridx=0; c.gridy=1;
		c.weighty=0;
		dimField.add(labDimY,c);
		
		dimY = new JSpinner();
		if(imgRatio > 1)
			dimY.setModel(new SpinnerNumberModel(10, 10, (int)Math.round(10000*imgRatio), 1));
		else
			dimY.setModel(new SpinnerNumberModel((int)Math.round(10*imgRatio), (int)Math.round(10*imgRatio), 10000, 1));
		
		dimY.setPreferredSize(new Dimension(70, 25));
		c.gridx=1; c.gridy=1;
		dimField.add(dimY,c);
		
		JLabel labIcon = new JLabel();
		try {
			labIcon.setIcon(new ImageIcon(ImageIO.read(new File("./images/images/main_window/link.png"))));
		} catch (IOException e) {}
		c.gridx=2; c.gridy=0;
		c.gridheight=2;
		dimField.add(labIcon,c);
		
		//Panel Découpage
		JLabel labNbX = new JLabel("x :");
		c.gridx=0; c.gridy=0;
		c.gridheight=1;
		c.weightx=0.62;
		c.weighty=1;
		c.anchor=GridBagConstraints.EAST;
		nbField.add(labNbX,c);
		
		nbX = new JSpinner();
		nbX.setModel(new SpinnerNumberModel(10, 1, 100, 1));
		nbX.setPreferredSize(new Dimension(70, 25));
		c.gridx=1; c.gridy=0;
		c.weightx=0.38;
		c.anchor=GridBagConstraints.WEST;
		nbField.add(nbX,c);
		
		JLabel labNbY = new JLabel("y :");
		c.gridx=0; c.gridy=1;
		c.weightx=0.62;
		c.anchor=GridBagConstraints.EAST;
		c.weighty=0;
		nbField.add(labNbY,c);
		
		nbY = new JSpinner();
		nbY.setModel(new SpinnerNumberModel(10, 1, 100, 1));
		nbY.setPreferredSize(new Dimension(70, 25));
		c.gridx=1; c.gridy=1;
		c.weightx=0.38;
		c.anchor=GridBagConstraints.WEST;
		nbField.add(nbY,c);
		
		//boutons
		btnVal = new JButton("Valider");
		btnCan = new JButton("Annuler");
		
		//layout Panel principal
		c.weightx=0;
		c.anchor = GridBagConstraints.CENTER;
		c.gridx=0; c.gridy=0;
		c.gridwidth=2;
		panel.add(dimField,c);
		
		c.gridy=1;
		panel.add(nbField,c);
		
		c.gridy=2;
		c.weighty=0.8;
		c.gridwidth=1;
		c.weightx=0.95;
		c.anchor=GridBagConstraints.EAST;
		panel.add(btnVal,c);
		
		c.gridx=1;
		c.weightx=0.05;
		panel.add(btnCan,c);
		
		getContentPane().add(panel);
		
		//listeners
		btnVal.addActionListener(this);
		btnCan.addActionListener(this);
		dimX.addChangeListener(this);
		dimY.addChangeListener(this);
		
		pack();
	}
	
	public boolean display(){
		setVisible(true);
		return isValidated;
	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		if(((JButton)evt.getSource()).equals(btnVal)){
			_simulator.getSenario().setMap(new Map((Integer)nbX.getValue(), (Integer)nbY.getValue(), img, imgName));
			isValidated=true;
		}
		setVisible(false);
		dispose();
	}
	
	@Override
	public void stateChanged(ChangeEvent arg0) {
		JSpinner spin = (JSpinner)arg0.getSource();
		try{
			if(spin.equals(dimX)){
				int yVal = Math.round((float)((Integer)dimX.getValue()/imgRatio));					
					
				if((Integer)dimY.getValue() != yVal)
					dimY.setValue(Integer.valueOf(yVal));
			}
			else{
				int xVal = Math.round((float)((Integer)dimY.getValue()*imgRatio));
				
				if((Integer)dimX.getValue() != xVal)
					dimX.setValue(Integer.valueOf(xVal));
			}
		}
		catch(ClassCastException e){}
	}
}
